package com.game.system;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.Callable;

import com.game.main.JStarCat;
import com.jme.image.Texture;
import com.jme.input.MouseInput;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Spatial;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueue;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.TextureKey;
import com.jme.util.TextureManager;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.XMLparser.Converters.FormatConverter;
import com.jmex.model.XMLparser.Converters.ObjToJme;

public class Utils {
	private static final String modelPath = "com/data/models/";
	private static final String texturePath = "com/data/texture/";
	public static Spatial loadModel(String name){
		URL model=JStarCat.class.getClassLoader().getResource(modelPath+name+".obj");
        FormatConverter converter=new ObjToJme();
        converter.setProperty("mtllib",model);
        ByteArrayOutputStream BO=new ByteArrayOutputStream();
        try {
        	TextureKey.setOverridingLocation(Utils.class.getClassLoader().getResource("com/data/texture/"));
            converter.convert(model.openStream(), BO);
            return (Spatial)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
        } catch (IOException e) {
        	
            e.printStackTrace();
            System.exit(0);
        }
        return null;
	}
	public static TextureState createTextureState(String name){
		TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().
				createTextureState();
        Texture t = TextureManager.loadTexture(getTexture(name), Texture.MM_LINEAR, 
        		Texture.FM_LINEAR);
		ts.setTexture(t);
		ts.setEnabled(true);
		return ts;
	}
	public static RenderState createMaterialState(ColorRGBA color) {
    	MaterialState material = 
    		DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
		material.setDiffuse(color);
		material.setAmbient(color);
		return material;
	}
	public static URL getTexture(String name){
		return JStarCat.class.getClassLoader().getResource(texturePath+name+".png");
	}
	@SuppressWarnings("unchecked")
	public static void setMouseVisible(boolean visible){
		final boolean isVisible=visible;
        Callable <?> exe = new Callable() {
            public Object call() {
            	if(isVisible){
                    MouseInput.get().setCursorVisible(true);
                    MouseInput.get().setHardwareCursor(Utils.getTexture("cursor"));
            	}
            	else{
            		MouseInput.get().setCursorVisible(false);
            	}
                return null;
            }
        };
		GameTaskQueueManager.getManager().getQueue(GameTaskQueue.RENDER).enqueue(exe);
	}
}